package com.laborder.sysyy.utils;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;


/**
 * @author 786107529@qq.com FJL
 * @Date 2018/10/11 13:46
 */
public class getIp {

    public  static String getClientIp(HttpServletRequest request) {
            String clientIp = request.getHeader("x-forwarded-for");
            if(clientIp == null || clientIp.length() == 0 || "unknown".equalsIgnoreCase(clientIp)) {
                clientIp = request.getHeader("Proxy-Client-IP");
            }
            if(clientIp == null || clientIp.length() == 0 || "unknown".equalsIgnoreCase(clientIp)) {
                clientIp = request.getHeader("WL-Proxy-Client-IP");
            }
            if(clientIp == null || clientIp.length() == 0 || "unknown".equalsIgnoreCase(clientIp)) {
                clientIp = request.getRemoteAddr()+request.getRemoteHost();
            }
        if (clientIp == null || clientIp.length() == 0 || clientIp.indexOf(":") > -1) {
            try {
                clientIp = InetAddress.getLocalHost().getHostAddress()+InetAddress.getLocalHost().getHostName();
            } catch (UnknownHostException e) {
                clientIp = null;
            }
        }

            return clientIp;
        }


}

